<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form :inline="true" size="medium">
      <el-form-item>
        <el-input v-model="searchObj.name" clearable placeholder="用户名" style="width:150px"/>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchObj.email" clearable placeholder="邮箱" prefix-icon="el-icon-message"
                  style="width:150px"
        />
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchObj.phone" clearable placeholder="手机号" prefix-icon="el-icon-mobile"
                  style="width:150px"
        />
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchObj.sex" clearable placeholder="性别" style="width:150px">
          <el-option value="1" label="男"/>
          <el-option value="2" label="女"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchObj.age" clearable placeholder="年龄" style="width:150px"/>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchObj.disabled" clearable placeholder="封禁状态" style="width:150px">
          <el-option value="0" label="正常"/>
          <el-option value="1" label="封禁"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button-group>
          <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
          <el-button type="warning" icon="el-icon-refresh-right" @click="resetData()">重置</el-button>
        </el-button-group>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table
      :data="list"
      :header-cell-style="{'text-align':'center'}"
      fit
      stripe
      tooltip-effect="light"
      v-loading="loading"
      highlight-current-row
      element-lodaing-text="数据加载中"
    >
      <el-table-column type="index" label="#" min-width="5%" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="disabled" label="状态" min-width="7%" align="center">
        <template slot-scope="scope">
          <el-tag v-if="Number(scope.row.disabled) === 1" effect="dark" type="danger" size="medium">封禁</el-tag>
          <el-tag v-else effect="dark" type="primary" size="medium">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="avatar" label="头像" min-width="8%" align="center">
        <template slot-scope="scope">
          <el-avatar :size="50" :src="scope.row.avatar" fit="contain" style="margin: 0 auto -6px;"/>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="用户名" min-width="10%" sortable align="center"/>
      <el-table-column prop="email" label="邮箱" min-width="15%" sortable show-overflow-tooltip>
        <template slot-scope="scope">
          <i class="el-icon-message"/>
          {{ scope.row.email }}
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="手机号" min-width="11%" sortable show-overflow-tooltip>
        <template slot-scope="scope">
          <i class="el-icon-mobile"/>
          {{ scope.row.phone }}
        </template>
      </el-table-column>
      <el-table-column prop="sex" label="性别" min-width="6%" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sex === 1" effect="plain" type="success" size="medium">男</el-tag>
          <el-tag v-else-if="scope.row.sex === 2" effect="plain" type="danger" size="medium">女</el-tag>
          <el-tag v-else effect="plain" type="info" size="medium">保密</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="age" label="年龄" min-width="7%" sortable align="center"/>
      <el-table-column prop="sign" label="签名" min-width="16%" sortable show-overflow-tooltip/>
      <el-table-column label="操作" min-width="15%" align="center">
        <template slot-scope="scope">
          <el-button-group>
            <el-button v-if="scope.row.disabled" type="primary" icon="el-icon-circle-check" size="small" plain
                       @click="update(scope.row.id)"
            >解封
            </el-button>
            <el-button v-else type="danger" icon="el-icon-circle-close" size="small" plain
                       @click="update(scope.row.id)"
            >封禁
            </el-button>
            <el-button type="success" icon="el-icon-delete" size="small" plain
                       @click="removeById(scope.row.id)"
            >删除
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-row>
      <el-col :span="24">
        <el-pagination
          :current-page="page"
          :total="total"
          :page-size="limit"
          :page-sizes="[5, 10, 15, 20, 50, 100]"
          style="padding: 30px 0; text-align: center;"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="changePageSize"
          @current-change="changeCurrentPage"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { memberApi } from '@/api'

export default {
  // 定义数据模型
  data() {
    return {
      loading: true, // 加载遮罩
      list: [], // 会员列表
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 10, // 每页记录数
      searchObj: {}// 查询条件
    }
  },
  computed: {},
  // 页面渲染后获取数据
  created() {
    this.fetchData()
  },
  // 定义方法
  methods: {
    fetchData() {
      // 开始加载
      this.loading = true
      // 调用api
      memberApi.queryByPage(this.page, this.limit, this.searchObj).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        // 结束加载
        this.loading = false
      })
    },
    // 封禁
    update(id) {
      memberApi.updateById(id).then(response => {
        this.fetchData()
        this.$message.success(response.message)
      })
    },
    removeById(id) {
      this.$confirm('将会删除这条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return memberApi.deleteById(id)
      }).then(() => {
        this.fetchData()
        this.$message.success('删除成功')
      }).catch(error => {
        // 当取消时会进入catch语句error = 'cancel'
        // 当后端服务抛出异常时error = 'error'
        if (error === 'cancel') {
          this.$message.info('取消删除')
        }
      })
    },
    // 更改每页记录数
    changePageSize(size) {
      this.limit = size
      this.fetchData()
    },
    // 更改页码
    changeCurrentPage(page) {
      this.page = page
      this.fetchData()
    },
    // 重置表单
    resetData() {
      this.searchObj = {}
      this.fetchData()
    }
  }
}
</script>

<style lang="scss">
@import "~@/assets/css/table_head_align.scss";
</style>
